//
//  MKUICollectionViewSectionModel.m
//  MKUI
//
//  Created by sushangjin on 14/12/29.
//  Copyright © 2020 sushangjin. All rights reserved.
//

#import "MKUICollectionViewSectionModel.h"
#import "MKUICollectionViewCellModel.h"
#import "MKUICollectionViewModel.h"

@implementation MKUICollectionViewSectionModel
- (id)init{
	if (self=[super init]) {
		_supplementaryElementCellClasses = [[NSMutableDictionary alloc] init];
	}
	return self;
}
- (id)copyWithZone:(NSZone *)zone{
    MKUICollectionViewSectionModel *obj = [super copyWithZone:zone];
    obj->_supplementaryElementCellClasses = [self->_supplementaryElementCellClasses mutableCopy];
    return obj;
}
- (__kindof MKUICollectionViewModel *)collectionModel{
	return (MKUICollectionViewModel *)[super collectionModel];
}
- (__kindof MKUICollectionViewCellModel *)cellModelAtIndex:(NSInteger)index{
 	return (MKUICollectionViewCellModel *)[super cellModelAtIndex:index];
}
- (UICollectionView *)collectionView{
	return ((MKUICollectionViewModel *)[self collectionModel]).collectionView;
}
- (void)refresh{
	NSIndexSet *set = [[self collectionModel] indexSetOfSectionModel:self];
	if(set){
		[self.collectionView reloadSections:set];
	}
}
- (void)setSupplementaryElementViewClass:(Class<MKUICollectionViewSupplementaryElementProtocol>)aClass forKind:(NSString *)kind{
    if(aClass==nil){
        [self removeSupplementaryElementViewClassForKind:kind];
        return;
    }
	if(![(Class)aClass isSubclassOfClass:[UICollectionReusableView class]])return;
	[_supplementaryElementCellClasses setObject:aClass forKey:kind];
}
- (void)removeSupplementaryElementViewClassForKind:(NSString *)kind{
	[_supplementaryElementCellClasses removeObjectForKey:kind];
}
- (Class<MKUICollectionViewSupplementaryElementProtocol>)supplementaryElementViewClassForKind:(NSString *)kind{
	Class<MKUICollectionViewSupplementaryElementProtocol> aClass = _supplementaryElementCellClasses[kind];
	return aClass;
}
- (void)displaySupplementaryElementView:(UICollectionReusableView<MKUICollectionViewSupplementaryElementProtocol> *)view forKind:(NSString *)kind{
	[view setCollectionSectionModel:self forKind:kind];
	[view setNeedsLayout];
}
@end
